﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RecruitingApplication.Data_Model;
using System.IO;

namespace RecruitingApplication.Services
{
    /// <summary>
    /// Summary description for Download
    /// </summary>
    public class Download : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int? docID = null;

            try
            {
                //attempt to get a candidate id or application id from the request url
                if (context.Request["docID"] != null)
                {
                    docID = Convert.ToInt32(context.Request["docID"]);
                }
            }
            catch { }

            //depending on which was id given, select the document from the db
            if (docID.HasValue)
            {
                DataAccessLayer.OpenDB();
                Document doc = DataAccessLayer.GetDocumentByID(docID.Value);

                if (doc != null)
                {
                    DownloadFile(doc.NoteBytes, doc.FileName, context.Response);
                }
                else
                {
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("File not found.");
                }
            }
            else
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("No file specified.");
            }
        }

        private void DownloadFile(byte[] bytes, string filename, HttpResponse response)
        {
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                long bytesToRead = ms.Length;
                response.ContentType = "application/octet-stream";
                response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                // Read the bytes from the stream in small portions. 
                while (bytesToRead > 0)
                {
                    // Make sure the client is still connected. 
                    if (response.IsClientConnected)
                    {
                        // Read the data into the buffer and write into the 
                        // output stream. 
                        byte[] buffer = new Byte[10000];
                        int length = ms.Read(buffer, 0, 10000);
                        response.OutputStream.Write(buffer, 0, length);
                        response.Flush();
                        // We have already read some bytes.. need to read 
                        // only the remaining. 
                        bytesToRead = bytesToRead - length;
                    }
                    else
                    {
                        // Get out of the loop, if user is not connected anymore.. 
                        bytesToRead = -1;
                    }
                }
            }

            response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}